<?php
require_once('../carbox.php');
if(!carbox_check_session( ))
	header('Location: http://'.$_SERVER['HTTP_HOST'].'/carbox/login.php');
switch($_POST['chart']) {
	case 1:
		$sql = "
			SELECT COUNT(p.id) AS qt, MONTH(d.created_on) AS m, YEAR(d.created_on) AS y
			FROM Page p INNER JOIN Document AS d ON p.document_id = d.id
			GROUP BY MONTH(d.created_on), YEAR(d.created_on)
			ORDER BY YEAR(d.created_on), MONTH(d.created_on)
		";
		break;
	case 2:
		$sql = "
			SELECT COUNT(d.id) AS qt, MONTH(d.created_on) AS m, YEAR(d.created_on) AS y
			FROM Document d
			GROUP BY MONTH(d.created_on), YEAR(d.created_on)
			ORDER BY YEAR(d.created_on), MONTH(d.created_on)
		";
		break;
	case 3:
		$sql = "
			SELECT COUNT(DISTINCT d.process_id) AS qt, MONTH(d.created_on) AS m, YEAR(d.created_on) AS y
			FROM Document d
			GROUP BY MONTH(d.created_on), YEAR(d.created_on)
			ORDER BY YEAR(d.created_on), MONTH(d.created_on)
		";
		break;
}

$query = end(carbox_query($sql));
$ret = array(array('Ano', 'Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'));
$data = array($query[0]['y'], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
$year = $query[0]['y'];
for($i = 0; $i < count($query); ++$i) {
	$data[$query[$i]['m']] = (int) $query[$i]['qt'];
	if($query[$i]['y'] != $year) {
		$ret[ ] = $data; 
		$data = array($query[$i]['y'], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
	}
}
$ret[ ] = $data; 
echo json_encode($ret);
?>
